package com.ruoyi.system.wnjk.service.impl;

import java.util.List;

import com.ruoyi.system.wnjk.domain.po.ShopItem;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.wnjk.mapper.ShopItemMapper;
import com.ruoyi.system.wnjk.service.IShopItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 商品信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-05
 */
@Service
public class ShopItemServiceImpl implements IShopItemService
{
    @Autowired
    private ShopItemMapper shopItemMapper;

    /**
     * 查询商品信息
     *
     * @param id 商品信息主键
     * @return 商品信息
     */
    @Override
    public ShopItem selectShopItemById(Long id)
    {
        return shopItemMapper.selectShopItemById(id);
    }

    /**
     * 查询商品信息列表
     *
     * @param shopItem 商品信息
     * @return 商品信息
     */
    @Override
    public List<ShopItem> selectShopItemList(ShopItem shopItem)
    {
        return shopItemMapper.selectShopItemList(shopItem);
    }

    /**
     * 新增商品信息
     *
     * @param shopItem 商品信息
     * @return 结果
     */
    @Override
    public int insertShopItem(ShopItem shopItem)
    {
        shopItem.setCreateTime(DateUtils.getNowDate());
        return shopItemMapper.insertShopItem(shopItem);
    }

    /**
     * 修改商品信息
     *
     * @param shopItem 商品信息
     * @return 结果
     */
    @Override
    public int updateShopItem(ShopItem shopItem)
    {
        shopItem.setUpdateTime(DateUtils.getNowDate());
        return shopItemMapper.updateShopItem(shopItem);
    }

    /**
     * 批量删除商品信息
     *
     * @param ids 需要删除的商品信息主键
     * @return 结果
     */
    @Override
    public int deleteShopItemByIds(Long[] ids)
    {
        return shopItemMapper.deleteShopItemByIds(ids);
    }

    /**
     * 删除商品信息信息
     *
     * @param id 商品信息主键
     * @return 结果
     */
    @Override
    public int deleteShopItemById(Long id)
    {
        return shopItemMapper.deleteShopItemById(id);
    }

    @Override
    public int downItem(Long[] ids) {
        return shopItemMapper.downItem(ids);
    }

    @Override
    public int putItem(Long[] ids) {
        return shopItemMapper.putItem(ids);
    }

    @Override
    public ShopItem getStockById(Long id) {
        return shopItemMapper.getStockById(id);
    }

    @Override
    public void increaseStock(Long itemId, Long quantity) {
        ShopItem shopItem = shopItemMapper.selectShopItemById(itemId);
        if (shopItem == null) {
            throw new RuntimeException("商品不存在");
        }
        // 增加库存
        shopItem.setStock((int) (shopItem.getStock() + quantity));
        shopItemMapper.updateShopItem(shopItem);
    }

    @Override
    public void lockStock(Long itemId, Long num) {
        ShopItem shopItem = shopItemMapper.selectShopItemById(itemId);
        if (shopItem == null) {
            throw new RuntimeException("商品不存在");
        }
        if (shopItem.getStock() < num) {
            throw new RuntimeException("库存不足");
        }
        // 扣减库存
        shopItem.setStock((int) (shopItem.getStock() - num));
        shopItemMapper.updateShopItem(shopItem);
    }
}
